﻿using FollowMe.Services;
using FollowMe.Services.Request;
using FollowMe.Services.Response;
using FollowMe.Services.View;
using Microsoft.AspNet.Identity;
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Web.Http;
using System.Web.Http.ModelBinding;

namespace FollowMe.API.Controllers
{
    /// <summary>
    /// Friend related API functions
    /// </summary>
    [RoutePrefix("api/Friend")]
    public class FriendController : ApiController
    {
        /// <summary>
        /// The  folllow me services
        /// </summary>
        private IFollowMeService _services;

        /// <summary>
        /// Initializes a new instance of the <see cref="FriendController"/> class.
        /// </summary>
        public FriendController()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FriendController"/> class.
        /// </summary>
        /// <param name="services">The services.</param>
        [InjectionConstructor]
        public FriendController(FollowMeService services)
        {
            _services = services;
        }

        //POST api/Friend/AddFriend
        /// <summary>
        /// Adds a new friend.
        /// </summary>
        /// <param name="request">The request/friend to add</param>
        /// <returns>the results of the addition</returns>
        [Route("AddFriend", Name = "AddFriend")]
        [AllowAnonymous]
        [System.Web.Http.AcceptVerbs("POST")]
        [System.Web.Http.HttpPost]
        public IHttpActionResult AddFriend(AddFriendRequest request)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest("Bad model");
            }

            FriendView friend = _services.addFriend(request);
            if (friend == null)
            {
                return NotFound();
            }
            return Ok(friend);
        }

        //DELETE api/Friend/RemoveFriend
        /// <summary>
        /// Removes the friend.
        /// </summary>
        /// <param name="memberid">The memberid deleting</param>
        /// <param name="exfriend">The exfriend to be deleted</param>
        /// <returns>the results of the deletion</returns>
        [Route("RemoveFriend", Name = "RemoveFriend")]
        [AllowAnonymous]
        [System.Web.Http.AcceptVerbs("DELETE")]
        [System.Web.Http.HttpDelete]
        public IHttpActionResult RemoveFriend(string memberid, string exfriend)
        {
            if (memberid == null) { return BadRequest("null"); }
            Guid memberGuid;
            memberid = memberid.Replace("(", "").Replace("{", "").Replace("}", "").Replace(")", "").Replace("-", "");
            if (!Guid.TryParseExact(memberid, "N", out memberGuid))
            {
                return BadRequest("Bad GUID");
            }

            if (exfriend == null) { return BadRequest("null"); }
            Guid exfriendGuid;
            exfriend = exfriend.Replace("(", "").Replace("{", "").Replace("}", "").Replace(")", "").Replace("-", "");
            if (!Guid.TryParseExact(exfriend, "N", out exfriendGuid))
            {
                return BadRequest("Bad GUID");
            }

            DeleteFriendRequest reqout = new DeleteFriendRequest
            {
                ExFriend = exfriendGuid,
                memberid = memberGuid
            };

            HTTPResults result = _services.deleteFriend(reqout);

            if (result.success == true) return Ok(result); else return BadRequest(result.statustext);
        }

        //GET api/Friend/Search
        /// <summary>
        /// Alls the information.
        /// </summary>
        /// <param name="search">(required)The search: a string looking for a username(email in this case)</param>
        /// <param name="memberid">(optional)The memberid of the member currently searching</param>
        /// <returns></returns>
        [Route("Search", Name = "Search")]
        [System.Web.Http.AcceptVerbs("GET")]
        [System.Web.Http.HttpGet]
        [Authorize]
        public FindFriendResponse AllInfo(string search, string memberid)
        {
            string id = User.Identity.GetUserId();

            if (id == null)
                return new FindFriendResponse {friends = new List<FriendView>() };

            Guid userID = Guid.Parse(User.Identity.GetUserId());

            FindFriendRequest req = new FindFriendRequest
            {
                memberid = userID,
                userName = search
            };

            return _services.searchForFriendsByUsername(req);
        }
        
    }
}